package controllers

import dao._
import play.api.mvc._
import play.api.routing.JavaScriptReverseRouter
import shared.VarTool
import tool.FormTool

import javax.inject.Inject
import scala.concurrent.ExecutionContext.Implicits.global

/**
 * Created by yz on 16/9/2021
 */
class AppController @Inject()(cc: ControllerComponents)(
  implicit val userDao: UserDao
) extends AbstractController(cc) {

  def toIndex = Action { implicit request =>
    println("in")
    Ok(views.html.index())
  }

  def loginBefore = Action { implicit request =>
    Ok(views.html.login())
  }

  def login = Action.async { implicit request =>
    val data = FormTool.userForm.bindFromRequest().get
    userDao.selectByUserData(data).map { optionUser =>
      if (optionUser.isDefined) {
        val user = optionUser.get
        val rSession = request.session + ("user" -> data.account) + (VarTool.idStr -> user.id.toString)
        Redirect(routes.AppController.toIndex()).withSession(rSession)
      } else {
        Redirect(routes.AppController.loginBefore()).flashing("info" -> "账号或密码错误!")
      }
    }
  }

  def javascriptRoutes = Action { implicit request =>
    Ok(
      JavaScriptReverseRouter("jsRoutes")(

        controllers.routes.javascript.UserController.accountCheck,

        controllers.routes.javascript.DataController.sampleNameCheck,
        controllers.routes.javascript.DataController.loadData,
        controllers.routes.javascript.DataController.getAllSample,
        controllers.routes.javascript.DataController.deleteSamples,
        controllers.routes.javascript.DataController.getSampleById,
        controllers.routes.javascript.DataController.sampleManage,
        controllers.routes.javascript.DataController.updateSample,

        controllers.routes.javascript.BlastController.blastn,
        controllers.routes.javascript.BlastController.blastnBefore,
        controllers.routes.javascript.BlastController.blastResultBefore,
        controllers.routes.javascript.BlastController.blastResult,
        controllers.routes.javascript.BlastController.downloadByName,
        controllers.routes.javascript.BlastController.downloadByRange,

        controllers.routes.javascript.CentrifugeController.missionNameCheck,
        controllers.routes.javascript.CentrifugeController.newMission,
        controllers.routes.javascript.CentrifugeController.missionManageBefore,
        controllers.routes.javascript.CentrifugeController.getAllMission,
        controllers.routes.javascript.CentrifugeController.getLogContent,
        controllers.routes.javascript.CentrifugeController.deleteMissions,
        controllers.routes.javascript.CentrifugeController.updateMissionSocket,
        controllers.routes.javascript.CentrifugeController.missionResultBefore,
        controllers.routes.javascript.CentrifugeController.getMissionById,
        controllers.routes.javascript.CentrifugeController.downloadResult,
        controllers.routes.javascript.CentrifugeController.getFileData,
        controllers.routes.javascript.CentrifugeController.getCpuNum,
        controllers.routes.javascript.CentrifugeController.reRunMissionBefore,
        controllers.routes.javascript.CentrifugeController.getAllReRunMissionNames,
        controllers.routes.javascript.CentrifugeController.getConfig,
        controllers.routes.javascript.CentrifugeController.reRunMission,
        controllers.routes.javascript.CentrifugeController.downloadData,
        controllers.routes.javascript.CentrifugeController.updateMission,
        controllers.routes.javascript.CentrifugeController.getStatInfo,


      )
    ).as("text/javascript")

  }


}
